package ua.dlab;


public class Algorithm {
	public static double[] doWork(double[][] matrix, int size, int steps) {
		double a[][] = new double[size][size];
        double b[] = new double[size];
        for (int i = 0; i < size; i++) {
        	for (int j = 0; j < size + 1; j++) {
        		if (j == size) {
        			b[i] = matrix[i][j] / matrix[i][i];
        		} else if (i != j) {
        			a[i][j] = -matrix[i][j] / matrix[i][i];
        		} else {
        			a[i][j] = 0;
        		}
        	}
        }
        
        double check = 0;
        for (int i = 0; i < size; i++) {
        	double currentSum = 0;
        	for (int j = 0; j < size; j++) {
        		currentSum += Math.abs(a[i][j]);
        	}
        	check = Math.max(check, currentSum);
        }
        
        if (check >= 1) {
        	throw new IllegalArgumentException("Достаття умова збіжності не виконується ||a|| = " + check);
        }
        
        double xk[] = b;
        double xkplus1[] = null;
        
        int counter = 0;
        while(counter < steps) {
        	xkplus1 = new double[size];
        	
        	for (int i = 0; i < size; i++) {
        		for (int j = 0; j < size; j++) {
        			xkplus1[i] += a[i][j] * xk[j];
        		}
        		
        		xkplus1[i] += b[i];
        	}
        	
        	counter++;
        	
        	xk = xkplus1;
        }
		
        return xkplus1;
	}
}